package com.situ.day8;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ExceptionDemo {
    @Test
    public void test111() {
        try {
            quQian(1900);
        } catch (MeiQianException e) {
            e.printStackTrace();
        }
    }

    public void quQian(double money) throws MeiQianException {
        if (money >= 1000) {
            throw new MeiQianException("钱不够了");
        }

        System.out.println("钱够了");
    }


    @Test
    public void test33() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test144() {
        try {
            show();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public void show() throws ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
    }



    @Test
    public void test1() {
        //java.lang.ArithmeticException: / by zero
        int num = 1 / 0;
        System.out.println("ExceptionDemo.test1");

        String str = null;
        System.out.println(str.toLowerCase());

    }

    @Test
    public void test44() {
        // 编译时异常：写代码时候必须要处理
        // 1、throws抛出  2、try-catch
        try {
            FileInputStream fileInputStream = new FileInputStream("a.txt");
            fileInputStream.read();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("ExceptionDemo.test44");

        /*try {
            FileInputStream fileInputStream = new FileInputStream("a.txt");
            fileInputStream.read();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (FileNotFoundException e) {
            //FileNotFoundException extends IOException
            //Exception 'java. io. FileNotFoundException' has already been caught
            throw new RuntimeException(e);
        }*/

    }

    @Test
    public void test2() {
        //java.lang.ArithmeticException: / by zero
        try {
            int num = 1 / 0;
        } catch (ArithmeticException e) {
            e.printStackTrace();
        } finally {
            System.out.println("ExceptionDemo.finally");
        }
        System.out.println("ExceptionDemo.test2");

        String str = null;
        if (str != null) {
            System.out.println(str.toLowerCase());
        }
    }
}
